//
//  YGEmoticonPageCell.swift
//  表情键盘
//
//  Created by liyonggui on 16/5/23.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

/// 一页按钮的个数 20
let YGEmoticonNumberOfPage = 20
/// 一页有几列  7
let YGEmoticonNumberOfCol = 7
/// 一页有几行  3
let YGEmoticonNumberOfRow = 3

protocol YGEmoticonPageCellDelegate: NSObjectProtocol {
    /// 表情按钮点击
    func emoticonPageCellDidClickEmoticonButton(emoticonModel: YGEmoticonModel)
    /// 删除点击按钮
    func emoticonPageCellDidClickDeleteButton()
}

class YGEmoticonPageCell: UICollectionViewCell {
    
    /// 代理属性
    weak var delegate: YGEmoticonPageCellDelegate?
    
    /// cell一页需要的模型
    var pageEmoticon: [YGEmoticonModel]? {
        didSet {
            // 拿到cell需要的一页数据
            
            // 隐藏所有按钮
            for button in emoticonButtons {
                button.hidden = true
            }
            
            // 遍历 获取 一个模型对应的按钮
            var index = 0
            for emoticonModel in pageEmoticon! {
                // 获取对应的按钮
                let button = emoticonButtons[index]
                // 传递模型给按钮
                button.emoticonModel = emoticonModel
                
                // 显示按钮
                button.hidden = false

                index++
            }
        }
    }
    
    /// 20个表情按钮数组
    private var emoticonButtons = [YGEmoticonButton]()
    
    /// indexPath
    var indexPath: NSIndexPath? {
        didSet {
            debugLabel.text = "第 \(indexPath!.section) 组, 第 \(indexPath!.item) 个 cell"
            recentLabel.hidden = (indexPath?.section != 0)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        
        // 添加20个按钮
        addEmotionButtons()
        
        // 添加最近label
        addSubview(recentLabel)
        
        // 添加删除按钮
        addSubview(deleteButton)
        
        // 调试label
        addSubview(debugLabel)
    }
    
    /// 添加20个按钮
    private func addEmotionButtons() {
        for _ in 0..<YGEmoticonNumberOfPage {
            let button = YGEmoticonButton()
            button.backgroundColor = UIColor.randomColor()
            
            // 设置大小
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            // 添加按钮事件
            button.addTarget(self, action: "didClickEmoticonButton:", forControlEvents: UIControlEvents.TouchUpInside)
            
            // 添加到父容器
            contentView.addSubview(button)
            // 添加到数组
            emoticonButtons.append(button)
        }
    }
    
    // 设置布局frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 左右间距
        let marginLR = 5
        let marginB = 25
        
        // 计算所有表情按钮的frame 
        /// 一个按钮宽度
        let width = (bounds.width - 2 * CGFloat(marginLR)) / CGFloat(YGEmoticonNumberOfCol)
        /// 一个按钮高度
        let height = (bounds.height - CGFloat(marginB)) / CGFloat(YGEmoticonNumberOfRow)
        
        var index = 0
        for button in emoticonButtons {
            // 计算在哪行
            let row = index / YGEmoticonNumberOfCol
            // 计算在哪列
            let col = index % YGEmoticonNumberOfCol
            
            let x = CGFloat(marginLR) + CGFloat(col) * width
            let y = CGFloat(row) * height
            
            button.frame = CGRect(x: x, y: y, width: width, height: height)
            
            index++
        }
        // 计算删除按钮frame
        let x = CGFloat(marginLR) + width * CGFloat(YGEmoticonNumberOfCol - 1)
        let y = height * CGFloat(YGEmoticonNumberOfRow - 1)
        deleteButton.frame = CGRect(x: x, y: y, width: width, height: height)
        
        // 最近label
        recentLabel.center = CGPoint(x: frame.width * 0.5, y: frame.height - recentLabel.frame.height)
    }
    
    // MARK: - 点击事件
    /// 表情按钮点击
    @objc private func didClickEmoticonButton(emoticonButton: YGEmoticonButton) {
        // 调用代理
        delegate?.emoticonPageCellDidClickEmoticonButton(emoticonButton.emoticonModel!)
    }
    /// 删除按钮点击
    @objc private func didClickDeleteButton() {
        // 调用代理
        delegate?.emoticonPageCellDidClickDeleteButton()
    }
    
    // MARK: - 懒加载
    /// debugLabel
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(30)
        label.frame = CGRect(x: 0, y: 30, width: 300, height: 100)
        
        return label
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        // 设置图片
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        
        // 点击事件
        button.addTarget(self, action: "didClickDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 最近label
    private lazy var recentLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.lightGrayColor()
        label.text = "最近使用表情"
        label.sizeToFit()
        
        return label
    }()
    
}































